package com.learn.blog.admin.service;

import com.learn.blog.admin.pojo.Admin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

import java.util.ArrayList;

/**
 * @Package: com.learn.blog.admin.service
 * @ClassName: SecurithService
 * @Author: 陈威
 * @CreateTime: 2021/9/14 21:33
 * @Description:
 */
@Component
public class SecurityUserService implements UserDetailsService {

    @Autowired
    AdminService adminService;


    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        //登录的时候会把username传递到这里
        //通过username查询到 admin表 如果admin存在 将密码告诉spring security
        //如果不存在 返回null 认证失败了
        Admin admin = adminService.findAdminByUserName(username);
        //登录失败
        if (admin==null){
            return null;
        }
        UserDetails userDetails=new User(username,admin.getPassword(),new ArrayList<GrantedAuthority>());


        return userDetails;
    }
}